import Vue from 'vue/dist/vue.common.prod.js'

export default function createVue(options = {}, Component, data, renderFn) {
  const methods = options.methods || {}
  delete options.methods
  options = {
    ...options,
    methods: {
      ...methods,
      init () {
        document.body.appendChild(this.$el)
      },
      destroy() {
        this.$destroy()
        document.body.removeChild(this.$el)
      }
    }
  }

  let renderData
  if (Component) {
    options.render = function render(createElement) {
      return createElement(Component, renderData, renderFn ? [renderFn(createElement)] : [])
    }
  }
  const instance = new Vue(options)
  instance.updateRenderData = function updateRenderData(data) {
    renderData = data
  }
  instance.updateRenderData(data)
  instance.$mount()
  instance.init()
  const [child] = instance.$children
  return child
}
